// import { routerRedux } from 'dva/router';
import { message } from 'antd';
// import {
//   reqFetchConstruction,
//   reqAddConstruction,
//   reqDelConstruction,
//   reqUpdateConstruction,
//   reqGetFence,
// } from './service';
import { history } from 'umi';

export default {
  namespace: 'chart',
  state: {
    data: [
      1,2,3,4,
    ],
  },
  reducers: {
    setList(state, payload) {
      // const constructionList = payload
      return { ...state, constructionList: payload.payload };
    },
    setFenceList(state, payload) {
      return { ...state, fenceList: payload.payload };
    },
  },
  effects: {
    *fetch({ payload }, { call, put }) {
      try {
        const response = yield call(reqFetchConstruction, payload);

        yield put({
          type: 'setList',
          payload: response.data,
        });
      } catch (err) {
        console.log(err);
      }
    },
    *add({ payload }, { call, put }) {
      const response = yield call(reqAddConstruction, payload);
      if (response.code === 0) {
        message.success('添加成功');
        yield put({
          type: 'fetch',
        });
        //请求fetch
      } else {
        message.error(response.msg);
      }
    },
    *fetchFence(_, { call, put }) {
      const res = yield call(reqGetFence);
      try {
        yield put({
          type: 'setFenceList',
          payload: res.data,
        });
      } catch (error) {
        message.error(error);
      }
    },
    *del({ payload }, { call, put }) {
      console.log(payload);
      const res = yield call(reqDelConstruction, payload);
      try {
        if (res.code === 0) {
          yield put({
            type: 'fetch',
          });
          message.success('删除成功');
        }
      } catch (error) {
        message.error(error);
      }
    },
  },
};
